#include "imx6u.h"

/**
 * 使能外设时钟
 */
void clk_enable(void)
{
	CCM->CCGR1 = 0xffffffff;
	CCM->CCGR2 = 0xffffffff;
	CCM->CCGR3 = 0xffffffff;
	CCM->CCGR4 = 0xffffffff;
	CCM->CCGR5 = 0xffffffff;
	CCM->CCGR6 = 0xffffffff;
}

void led_init(void)
{
	IOMUX_SW_MUX->GPIO1_IO03 = 0x5;
	IOMUX_SW_PAD->GPIO1_IO03 = 0x10b0;

	GPIO1->GDIR = 0x8;
	GPIO1->DR = 0x0;
}

/* 短延迟 */
void delay_short(volatile unsigned int n)
{
	while (n--)
		;
}

/**
 * 延时，一次循环大概是1ms,再主频396MHz
 * @param       延迟n秒
 */
void delay(volatile unsigned int n)
{
	while (n--)
	{
		delay_short(0x7ff);
	}
}

/**
 * 打开LED灯
 */
void led_on(void)
{
	GPIO1->DR &= ~(1 << 3);
}

/**
 * 关闭LED灯
 */
void led_off(void)
{
	GPIO1->DR |= (1 << 3);
}

int main(void)
{
	/* 初始化外设时钟 */
	clk_enable();
	/* 初始化LED */
	led_init();
	/* 设置LED闪烁 */
	while (1)
	{
		led_on();
		delay(500);
		led_off();
		delay(500);
	}
	return 0;
}